package cn.broadmax.boot.modules.sys.controller;


import cn.broadmax.boot.base.SuperController;
import cn.broadmax.boot.base.message.SResult;
import cn.broadmax.boot.log.SysLog;
import cn.broadmax.boot.modules.sys.bean.area.SysAreaSearch;
import cn.broadmax.boot.modules.sys.entity.SysArea;
import cn.broadmax.boot.modules.sys.mapper.SysAreaMapper;
import cn.broadmax.boot.modules.sys.service.ISysAreaService;
import cn.broadmax.boot.util.SAssert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jexx.util.Console;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 部门 前端控制器
 * </p>
 *
 * @author jexx
 * @since 2021-07-01
 */
@Api(tags = "区域")
@RestController
@RequestMapping("/sys/area")
@RequiredArgsConstructor
public class SysAreaController extends SuperController {
    @Autowired
    private final ISysAreaService iSysAreaService;

    @Autowired
    private final SysAreaMapper sysAreaMapper;

    @ApiOperation("新增部门")
    @SysLog(value = "新增部门")
    @PutMapping(value = "/insert")
    public SResult<SysArea> addDepartment(@RequestBody SysArea department){
        Console.log("111111111111");
        SAssert.hasText(department.getDepartName(), "组织名称不能为空");
        SAssert.hasText(department.getOrgCode(), "组织编码不能为空");
        SysArea exist = iSysAreaService.getDepartmentByCode(department.getOrgCode());
        SAssert.isNull(exist, "组织编码已存在");
        iSysAreaService.save(department);

        department = iSysAreaService.getById(department.getId());
        return SResult.ok("", department);
    }

    @ApiOperation("根据主键删除组织/部门")
    @SysLog(value = "删除组织/部门")
    @DeleteMapping(value = "/delete/{departmentId}")
    public SResult<Boolean> deleteDepartmentById(@PathVariable String departmentId){
        boolean success = iSysAreaService.removeById(departmentId);
        return SResult.ok("", success);
    }

    @ApiOperation("根据主键更新部门, 组织编码不能更新")
    @SysLog(value = "更新组织/部门")
    @PutMapping(value = "/update/{departmentId}")
    public SResult<SysArea> updateDepartmentById(@PathVariable String departmentId, @RequestBody SysArea department){
        System.out.println("-------111111");
        System.out.println(department.toString());
        SAssert.notNull(department);

        iSysAreaService.updateById(department);

        department = iSysAreaService.getById(department.getId());
        return SResult.ok("", department);
    }

    @ApiOperation("分页查询部门")
    @GetMapping(value = "")
    public SResult<IPage<SysArea>> pageRoles(SysAreaSearch search){
        LambdaQueryWrapper<SysArea> wrapper = SysAreaSearch.createWrap(search);
        IPage<SysArea> page = iSysAreaService.page(getPage(), wrapper);
        return SResult.ok("", page);
    }
}
